import { Command, Option, Positional } from "nestjs-command";
import { Injectable } from "@nestjs/common";
import { AccountsService } from "./accounts.service";
import { Role } from "@/enums";

@Injectable()
export class AccountCommand {
    constructor(private readonly accountService: AccountsService) {}

    @Command({
        command: "create:account <username>",
        describe: "Создать новую учетную запись"
    })
    async create(
        @Positional({
            name: "username",
            type: "string"
        })
        userName: string,
        @Option({
            name: "password",
            type: "string",
            alias: "p",
            demandOption: true,
            requiresArg: true
        })
        password: string,
        @Option({
            name: "email",
            type: "string",
            alias: "e",
            demandOption: true,
            requiresArg: true
        })
        email: string,
        @Option({
            name: "roles",
            type: "array",
            alias: "r",
            demandOption: true,
            requiresArg: true,
            array: true,
            choices: [Role.Admin, Role.Teacher, Role.Student]
        })
        roles: string[]
    ) {
        try {
            await this.accountService.create({
                userName,
                password,
                email,
                roles
            });
            console.log("Учетная запись создана");
        } catch (e) {
            console.error(`Ошибка создания учетной записи: ${e.message}`);
        }
    }
}
